package com.atguigu.gmall.payment.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.payment.service.ZfbPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/15 16:03 星期六
 * description: 支付宝支付相关接口的控制层
 */
@RestController
@RequestMapping(value = "/zfb/pay")
public class ZfbPayController {

    @Autowired
    private ZfbPayService zfbPayService;

    /**
     * 获取支付宝支付页面的api
     * @param body
     * @param orderId
     * @param money
     * @return
     */
    @GetMapping(value = "/getPayPage")
    public String getPayPage(@RequestParam String body,
                             @RequestParam String orderId,
                             @RequestParam String money){
        return zfbPayService.getPayPage(body,orderId,money);
    }

    /**
     * 查询订单的支付结果
     * @param orderId
     * @return
     */
    @GetMapping("/getPayResult")
    public String getPayResult(@RequestParam String orderId){
        return zfbPayService.getPayResult(orderId);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 异步通知的接口: 在用户在支付宝中付款完成后支付宝调用的接口
     * @param notifyData
     * @return
     */
    @RequestMapping(value = "/notify/address")
    public String notifyAddress(@RequestParam Map<String,String> notifyData){
        //System.out.println("异步通知的内容为: " + JSONObject.toJSONString(notifyData));

        String result = "{\"gmt_create\":\"2022-10-14 14:23:32\",\"charset\":\"utf-8\",\"gmt_payment\":\"2022-10-14 14:23:36\",\"notify_time\":\"2022-10-14 14:23:37\",\"subject\":\"温毛小吉吉\",\"sign\":\"Wr8YvnpT2lfkEUYjl9q/P+8qIYfc6z+6TdgIqL/toeKlzpvSFE6lwqgzA4faECe/2xclH/d3+085xgcLDk1wJ1j994LZnUtjHWO9lcvofejdv+l92ifULA1rf9gzRm53JIrhdw+TE/ewvd4eChyLspj5HjnijrH1HUMO7HhJa94yYp024QQeA3+H5y85iMkf+t2J0e1Xw8sYKxYoQga09ROCqCldJE+y3w46v9Ofp4j6ZWbO8iQOUNpmZoP0Y4hkvZhwNWcexnb1SR8qoCgMGszlD2C07+6vVn02O10FMzX0wp5vcb21mssxuGq4z1KFnrUl6Vm/HNa2/3wuerz8Aw==\",\"buyer_id\":\"2088922156020423\",\"invoice_amount\":\"0.01\",\"version\":\"1.0\",\"notify_id\":\"2022101401222142337020421461350275\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.01\\\",\\\"fundChannel\\\":\\\"ALIPAYACCOUNT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"out_trade_no\":\"257\",\"total_amount\":\"0.01\",\"trade_status\":\"TRADE_SUCCESS\",\"trade_no\":\"2022101422001420421406644021\",\"auth_app_id\":\"2021001163617452\",\"receipt_amount\":\"0.01\",\"point_amount\":\"0.00\",\"buyer_pay_amount\":\"0.01\",\"app_id\":\"2021001163617452\",\"sign_type\":\"RSA2\",\"pay_way\":\"ZFB\",\"seller_id\":\"2088831489324244\"}";
        rabbitTemplate.convertAndSend("pay_exchange","order.pay",result);
        return "SUCCESS";
    }

    /**
     * 同步回调: 用户付款完成以后,引导用户页面跳转的地址
     * @return
     */
    @RequestMapping(value = "/return/address")
    public String returnAddress(@RequestParam Map<String,String> returnData){
        System.out.println("同步跳转的内容为: " + JSONObject.toJSONString(returnData));
        return "同步回调成功!!!!!!";
    }

}
